package com.hexb.smh.aop;

import com.hexb.core.utils.ObjectUtils;
import com.hexb.smh.entity.enums.ILogCreator;
import com.hexb.smh.entity.enums.Operators;
import com.hexb.smh.entity.view.ProductView;
import com.hexb.smh.service.IProductService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : hexb
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DeleteProductLogCreator implements ILogCreator, InitializingBean {

    @NonNull
    private final OperationLogFactory factory;

    @NonNull
    private final IProductService productService;

    @Override
    public String create(Object[] args) {
        Integer id = (Integer) args[0];
        ProductView productView = productService.selectOneById(id);
        if (productView == null) {
            return String.format("删除项目:[%s]", args[0]);
        }
        return String.format("删除项目:[%s]([%s])", productView.getName(), productView.getItemNo());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        factory.register(Operators.deleteProduct, this);
    }
}
